/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "Hogshead.Common.h"

#include <Windows.h>

#include "../Content/Scene.h"

namespace Hogshead
{
	namespace Core
	{
		/**
		* The Game class will be the root class for a Game using the Hogshead Engine.  It will set up a lot of stuff by default
		* and also provides a virtual initialize method to setup any other Manager type objects which are made down the road.
		*/

		typedef void (*PredrawCallback)(void);

		class  IGame
		{
		public:
			IGame();
			virtual ~IGame();

			

			bool initialize(int in_width, int in_height);//assumes we want to create a window for our app
			bool initialize(HWND in_window_handle);//assumes we are being given a window to render to for our app
			void finalize();
			
			// returns pointer to the current scene
			Scene* getCurrentScene() const;

			// run the game durrr
			void run();
			void draw();

			static void exit();
			
			//loads a scene and adds a reference to it in our game's _scene_dictionary, returns the key
			static const String& loadScene(const String& in_filepath, bool prepend_environment_path = true);
			//this will be used to unload a scene and it's content when we are done with it
			static bool unloadScene(const String& in_key);

			static void reloadScene(const String& in_key);

			static void getPreviousScenePath(String& out_key);

			//changes which scene is running
			static void changeScene(const String& in_filepath);
			//unloads current scene, loads new scene, and runs new scene
			static void transistScene(const String& in_filepath);

			static bool getCurrentSceneKey(String& out_key);

			// register a windows event callback function with the game
			virtual void registerWindowsEventCallback(WNDPROC in_proc);

			virtual void registerPredrawCallback(PredrawCallback in_callback)
			{
				_manager_predraw_callback.add(in_callback);
			}

			void setMultithreadUpdate(bool use)
			{
				_multithread_update = use;
			}
			__forceinline bool getMultithreadUpdate() const
			{
				return _multithread_update;
			}

			void setTargetFramerate(int in_rate)
			{
				_target_framerate = in_rate;
			}
		protected:
			void quit();

			/** User/Gameplay **/

			virtual void configure() = 0;

			virtual void initializeEngineLibraries() = 0;
			virtual void finalizeEngineLibraries() = 0;

			virtual void initializeGameplayLibraries() = 0;
			virtual void finalizeGameplayLibraries() = 0;
			
			// load the content into the resource library (textures, sound, scripts, etc)
			virtual void loadContent() = 0;
			// pure virtual method to load the Scenes into memory
			virtual void selectStartScene() = 0;


			
			Vector<WNDPROC> _manager_callback;
			Vector<PredrawCallback> _manager_predraw_callback;

			// its a game timer
			GameTimer* _game_timer;

			// the current scene we update on each run
			Scene* _current_scene;

			void update(const GameTimer& in_timer);

			/** Windows **/

			static LRESULT CALLBACK wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
			LRESULT CALLBACK handleWindowsMessage(HWND p_window_handle, UINT p_message, WPARAM p_word, LPARAM p_long_word);
			bool createWindow(HINSTANCE p_hInstance, int p_width, int p_height); 
			HWND _hWnd;

			void pauseSystem();
			void unPauseSystem();
			bool _system_paused;

			void safeTransistToScene(const String& in_filepath);
			void safeChangeToScene(const String& in_filepath);
			void safeReloadScene(const String& in_key);

			bool _multithread_update;
			int _target_framerate;

			// this flag gets set when the user tells the game to quit
			bool _should_quit;

		private:
			static IGame* _game;			

			String _new_scene_filepath;
			String _reload_scene_key;

			bool _reload_scene;

			bool _transist;
			bool _change;

			float _last_draw_time;
			bool _has_finalized;

			static String _previous_scene_path;
		};
	}
}